package com.codergd.gra.alipay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.codergd.gra.config.Mesg;
import org.springframework.stereotype.Component;

/**
 * 创建交易订单
 * 返回
 */
@Component
public class AlipayTradePrecreate {
//    public AlipayTradePrecreate() throws AlipayApiException {
//
//    }

    public String creatOrder(String no,float num) throws AlipayApiException{
        //创建客户端
        AlipayClient alipayClient = new DefaultAlipayClient(Mesg.URL,Mesg.APPID,
                Mesg.APP_PRIVATE_KEY,"json","GBK",
                Mesg.ALIPAY_PUBLIC_KEY,"RSA2");

        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl("");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", no);
        bizContent.put("total_amount", num);
        bizContent.put("subject", "测试商品");

        request.setBizContent(bizContent.toString());
        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
//        System.out.println(response.getBody());
//        System.out.println("___");
        return response.getQrCode();
    }
}
